/**
 * 节点注册管理相关 API
 */

import { get, post, del } from '@/utils/request'

/**
 * 注册令牌信息
 */
export interface RegistrationToken {
  tokenId: number
  token: string
  name: string
  description: string
  expiresAt: string
  maxUsageCount: number
  usedCount: number
  isRevoked: boolean
  revokedReason: string | null
  createdBy: string
  createdAt: string
  registrationCommand: string
  registrationLink: string
}

/**
 * 生成注册令牌请求
 */
export interface GenerateTokenRequest {
  name: string
  description: string
  expiresInHours: number
  maxUsageCount: number
  createdBy: string
}

/**
 * 验证令牌请求
 */
export interface ValidateTokenRequest {
  token: string
}

/**
 * 验证令牌响应
 */
export interface ValidateTokenResponse {
  isValid: boolean
  errorMessage: string | null
}

/**
 * 撤销令牌请求
 */
export interface RevokeTokenRequest {
  reason: string
  revokedBy: string
}

/**
 * 生成注册令牌
 */
export function generateToken(data: GenerateTokenRequest): Promise<RegistrationToken> {
  return post<RegistrationToken>('/api/NodeRegistration/token', data)
}

/**
 * 获取所有令牌
 */
export function getTokens(includeRevoked = false): Promise<RegistrationToken[]> {
  return get<RegistrationToken[]>('/api/NodeRegistration/token', {
    params: { includeRevoked },
  })
}

/**
 * 获取令牌详情
 */
export function getTokenById(tokenId: number): Promise<RegistrationToken> {
  return get<RegistrationToken>(`/api/NodeRegistration/token/${tokenId}`)
}

/**
 * 验证令牌
 */
export function validateToken(data: ValidateTokenRequest): Promise<ValidateTokenResponse> {
  return post<ValidateTokenResponse>('/api/NodeRegistration/token/validate', data)
}

/**
 * 撤销令牌
 */
export function revokeToken(tokenId: number, data: RevokeTokenRequest): Promise<void> {
  return post<void>(`/api/NodeRegistration/token/${tokenId}/revoke`, data)
}

/**
 * 删除令牌
 */
export function deleteToken(tokenId: number): Promise<void> {
  return del<void>(`/api/NodeRegistration/token/${tokenId}`)
}

/**
 * 获取注册指南
 */
export function getRegistrationInstructions(): Promise<string> {
  return get<string>('/api/NodeRegistration/instructions')
}
